import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class timeLimit {
    public static void limitTime() {
        JFrame frame = new JFrame("限时模式");
        frame.setSize(1000,700);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new GridLayout(5,1,10,10));
        //
        JLabel title = new JLabel("请选择时间和难度");
        title.setFont(new Font("宋体",Font.CENTER_BASELINE,16));
        frame.add(title);
        //按钮
        JButton oneButton = new JButton("9×9 10雷 - 2分钟");
        JButton twoButton = new JButton("16×16 25雷 - 5分钟");
        JButton cancelButton = new JButton("退出限时模式");
        //自定义模式 已完善
        JButton threeButton = new JButton("自定义模式");
        //事件跳转
        oneButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                //生成方法此处
                limitTimeGame(9,9,10,120);
            }
        });
        twoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                //生成方法此处
                limitTimeGame(16,16,25,300);
            }
        });
        threeButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent a) {
                        int rows=0,cols=0,tntNums=0,times=0;
                        //判断 可直接用自定义
                        try{
                            String inputRows,inputCols,inputTnt,inputTime;
                            inputRows=JOptionPane.showInputDialog(null, "请输入行数(最小9,最大24):", "自定义行数", JOptionPane.PLAIN_MESSAGE);
                            inputCols=JOptionPane.showInputDialog(null, "请输入列数(最小9,最大30):", "自定义列数", JOptionPane.PLAIN_MESSAGE);
                            inputTnt=JOptionPane.showInputDialog(null, "请输入地雷数量:", "自定义地雷数量", JOptionPane.PLAIN_MESSAGE);
                            inputTime=JOptionPane.showInputDialog(null, "请输入时间（秒）:", "自定义时间", JOptionPane.PLAIN_MESSAGE);
                            rows=Integer.parseInt(inputRows.trim());
                            cols=Integer.parseInt(inputCols.trim());
                            tntNums=Integer.parseInt(inputTnt.trim());
                            times=Integer.parseInt(inputTime.trim());
                            //判断输入值是否有效
                            if(rows<9||rows>24){
                                rows=9;
                            }
                            if(cols<9||cols>30){
                                cols=9;
                            }
                            if(tntNums<1||tntNums>=(rows*cols)){
                                tntNums=10;
                            }
                            if(times<30){
                                times=30;
                            }
                        }catch (Exception e){
                            JOptionPane.showMessageDialog(null, "输入无效，使用默认初级设置9*9 10雷 - 2分钟");
                            limitTimeGame(9,9,10,120);

                        }
                        frame.dispose();
                        //生成方法此处
                        limitTimeGame(rows,cols,tntNums,times);
                    }
                });
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });
        //按钮添加
        frame.add(oneButton);
        frame.add(twoButton);
        frame.add(threeButton);
        frame.add(cancelButton);
        frame.setVisible(true);
    }
    public static void limitTimeGame(int rows,int cols,int tntNum,int limitT) {
        JFrame limitframe = new JFrame("狂暴雷域 - 未知の危险");
        limitframe.setSize(1000, 700);
        limitframe.setLocationRelativeTo(null);
        limitframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        gamePanel panel = new gamePanel(rows, cols, tntNum);
        //设定时限
        panel.setTimeLimit(limitT);
        limitframe.setContentPane(panel);
        limitframe.setVisible(true);
    }
}
